<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="app-nav-bar">
      <van-button
        slot="title"
        class="search-btn"
        icon="search"
        type="info"
        round
        size="small"
        to="/search"
      >搜索</van-button>
    </van-nav-bar>
    <!-- /导航栏 -->
    <!-- 文章频道 -->
    <van-tabs class="channel-tabs" v-model="active">
      <!-- 只有第一次查看标签页才会渲染 -->
      <van-tab
       :title="channel.name"
       v-for="channel in channels"
       :key="channel.id"
      >
      <article-list :channel="channel"/>
      </van-tab>
      <div slot="nav-right"
        class="wap-nav-palceholder">
      </div>
      <div
       slot="nav-right"
       @click="isChannelEditShow=true"
       class="wap-nav-wrap"
      >
        <van-icon name="wap-nav"/>
      </div>
    </van-tabs>
    <!-- 文章频道 -->
    <!-- get-container="body" 直接挂在到body不属于现在的节点 -->
    <van-popup
      v-model="isChannelEditShow"
      position="bottom"
      class="channel-edit-popup"
      closeable
      close-icon-position="top-left"
      get-container="body"
      style="height: 100%"
    >
    <!--&event 就是事件参数-->
      <channel-edit
       :user-channels="channels"
       :active="active"
       @close="isChannelEditShow=false"
       @update-active="onUpdateActivce"/>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user'
import { getItem } from '@/utils/storage'
import ArticleList from './components/article-list'
import ChannelEdit from './components/channel-edit'
export default {
  name: 'HomeIndex',
  components: {
    ArticleList,
    ChannelEdit
  },
  props: {},
  data () {
    return {
      active: 0,
      channels: [],
      isChannelEditShow: false
    }
  },
  computed: {},
  watch: {},
  created () {
    this.loadChannels()
  },
  mounted () {},
  methods: {
    async loadChannels () {
      try {
        let channels = []
        if (this.user) {
          // 已登录，请求获取线上的频道数据
          const { data } = await getUserChannels()
          channels = data.data.channels
        } else {
          // 未登录
          const localChannels = getItem('channels')
          if (localChannels) {
            // 有本地频道数据，则使用
            channels = localChannels
          } else {
            // 没有本地频道数据，则请求获取默认推荐的频道列表
            const { data } = await getUserChannels()
            channels = data.data.channels
          }
        }
        // 将数据更新到组件中
        this.channels = channels
      } catch (err) {
        console.log(err)
        this.$toast('数据获取失败')
      }
    },
    onUpdateActivce (index) {
      this.active = index
    }
  }
}
</script>

<style scoped lang="less">
.home-container {
  /deep/ .van-nav-bar__title {
    max-width: none;
  }
  .search-btn {
    width: 277px;
    height: 32px;
    background: #5babfb;
    border: none;
    .van-icon {
      font-size: 16px;
    }
    .van-button__text {
      font-size: 14px;
    }
  }
  .channel-tabs{
    /deep/ .van-tab{
      border-right: 1px solid #edeff3;
      border-bottom: 1px solid #edeff3;
    }
    /deep/ .van-tabs__line{
      width: 15px !important;
      height: 3px;
      background: #3296fa;
      bottom: 20px;
    }
  }
  .wap-nav-palceholder{
    width: 33px;
    flex-shrink: 0;
  }
  .wap-nav-wrap{
    position: fixed;
    right: 0;
    height: 43px;
    width: 33px;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #fff;
    opacity:.9;
    .van-icon{
      font-size: 26px;
    }
  &:before{
    content: '';
    position:absolute;
    top: 0;
    left: 0;
    width: 2px;
    height: 33px;
    // background: -webkit-linear-gradient(left, black, transparent);
  }
  }
}
</style>
